<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<meta http-equiv="Cache-Control" content="no-siteapp" />

    <title>tbox stream流的使用</title>
    <meta name="description" content="stream是tbox的最常用的流，一般用于单路io操作，既可以进行阻塞读写，也可以非阻塞的读写。目前可以支持 数据、文件、套接字、http协议以及各种过滤器的读写操作，也可以很方便的自定义扩展自己的流模块。之前已经在tbox数据位操作接口的使用一文中，简单介绍了下stream的位读写接口，这里继续详细介绍下st...">

    
    <meta name="keywords" content="tbox,stream,通用流,tboox" /> 

    <!-- qq oauth -->
    <meta property="qc:admins" content="5211601217706727767255" />

    <!--icon -->
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon">
    <link rel="icon" sizes="192x192" href="/static/img/nice-highres.png" />
	<link rel="apple-touch-icon-precomposed" href="/static/img/apple-touch-icon-57x57-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="72x72" href="/static/img/apple-touch-icon-72x72-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="114x114" href="/static/img/apple-touch-icon-114x114-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/img/apple-touch-icon-144x144-precomposed.png" />
	<link rel="apple-touch-icon-precomposed" sizes="180x180" href="/static/img/retinahd_icon.png" />
	<meta name="msapplication-TileImage" content="/static/img/retinahd_icon.png" />
	
    <link rel="stylesheet" href=" /css/fontawesome/css/font-awesome.min.css ">
    <link rel="stylesheet" href=" /css/main.css ">
    <link rel="canonical" href="https://tboox.org/cn/2016/08/25/stream-rw/">
    <link rel="alternate" type="application/rss+xml" title="TBOOX Open Source Project" href="https://tboox.org/feed.xml ">
    <link rel="alternate" hreflang="en" href="https://tboox.org/" />
    <link rel="alternate" hreflang="zh-Hans" href="https://tboox.org/cn/" />

    <!-- css -->
    <link href="/css/reward.css" rel="stylesheet" type="text/css"> 




    <script type="text/javascript">
    function isPC(){    
        var userAgentInfo = navigator.userAgent;  
        var Agents = new Array("Android", "iPhone", "SymbianOS", "Windows Phone", "iPad", "iPod");    
        var flag = true;    
        for (var v = 0; v < Agents.length; v++) {    
            if (userAgentInfo.indexOf(Agents[v]) > 0) { flag = false; break; }    
        }    
        return flag;    
    }
    </script>

<!-- baidu ads -->



    <!-- baidu ads -->

</head>


  <body>

    <header id="top">
    <div class="wrapper">
        <a href="/cn" class="brand">TBOOX</a>
        <button id="headerMenu" class="menu"><i class="fa fa-bars"></i></button>
        <nav id="headerNav">
            <ul>
                <li>
                    
                    <a href="/?lang=0">
                    
                        <i class="fa fa-home"></i>English
                    </a>
                </li>

                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/project/">
                            
                        
                            <i class="fa fa-bookmark"></i>项目
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/archive/">
                            
                        
                            <i class="fa fa-archive"></i>归档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/category/">
                            
                        
                            <i class="fa fa-th-list"></i>分类
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/tag/">
                            
                        
                            <i class="fa fa-tags"></i>标记
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/docs/">
                            
                        
                            <i class="fa fa-book"></i>文档
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="https://xmake.io/#/zh-cn/about/contact" target="_blank" >
                            
                        
                            <i class="fa fa-forumbee"></i>社区
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/donation/">
                            
                        
                            <i class="fa fa-heart"></i>捐助
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                    
                    <li>
                        
                            
                            <a href="/cn/about/">
                            
                        
                            <i class="fa fa-user"></i>关于
                        </a>
                    </li>
                    
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                    
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
                    
                
            </ul>
        </nav>
    </div>
</header>



        <div class="page clearfix" post>
    <div class="left">
        <h1>tbox stream流的使用</h1>
        <div class="label">

            <div class="label-card">
                <i class="fa fa-calendar"></i>2016-08-25
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
                
            </div>

            <div class="label-card">
            


<!-- <span class="point">•</span> -->
<span class="categories">
  <i class="fa fa-th-list"></i>
  
    
        <a href="/category/#tbox" title="Category: tbox" rel="category">tbox</a>
    
  

  <!-- <span class="point">•</span> -->
</span>


            </div>

            <div class="label-card">
            
<!-- <span class="point">•</span> -->
<span class="pageTag">
  <i class="fa fa-tags"></i>
  
    
        <!--a href="/tag/#tbox" title="Tag: tbox" rel="tag">tbox</a-->
        <a href="/cn/tag/#tbox" title="Tag: tbox" rel="tag">tbox</a>&nbsp;
    
        <!--a href="/tag/#stream" title="Tag: stream" rel="tag">stream</a-->
        <a href="/cn/tag/#stream" title="Tag: stream" rel="tag">stream</a>&nbsp;
    
        <!--a href="/tag/#%E9%80%9A%E7%94%A8%E6%B5%81" title="Tag: 通用流" rel="tag">通用流</a-->
        <a href="/cn/tag/#通用流" title="Tag: 通用流" rel="tag">通用流</a>
    
  

</span>

            </div>

        </div>
        <hr>
        <article itemscope itemtype="http://schema.org/BlogPosting">
        <p>stream是tbox的最常用的流，一般用于单路io操作，既可以进行阻塞读写，也可以非阻塞的读写。</p>

<p>目前可以支持 数据、文件、套接字、http协议以及各种过滤器的读写操作，也可以很方便的自定义扩展自己的流模块。</p>

<p>之前已经在<a href="/cn/2016/08/12/bits-operation/">tbox数据位操作接口的使用</a>一文中，简单介绍了下stream的位读写接口，这里继续详细介绍下stream的常用数据读写接口。</p>

<h4 id="流的常用初始化操作">流的常用初始化操作</h4>

<p>下面直接上代码吧，基本上看下注释就知道怎么使用了，嘿嘿。。。</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 初始化文件流
</span>    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_from_url</span><span class="p">(</span><span class="s">"/root/home/file"</span><span class="p">);</span>
    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_from_file</span><span class="p">(</span><span class="s">"/root/home/file"</span><span class="p">,</span> <span class="n">TB_FILE_MODE_RW</span> <span class="o">|</span> <span class="n">TB_FILE_MODE_CREAT</span> <span class="o">|</span> <span class="n">TB_FILE_MODE_BINARY</span> <span class="o">|</span> <span class="n">TB_FILE_MODE_TRUNC</span><span class="p">);</span>

    <span class="c1">// 初始化http流
</span>    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_from_url</span><span class="p">(</span><span class="s">"http://www.xxxx.com/file?args=xxx"</span><span class="p">);</span>
    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_from_http</span><span class="p">(</span><span class="s">"www.xxxx.com"</span><span class="p">,</span> <span class="mi">80</span><span class="p">,</span> <span class="s">"/file?args=xxx"</span><span class="p">,</span><span class="n">tb_false</span> <span class="p">);</span>

    <span class="c1">// 初始化tcp流
</span>    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_from_url</span><span class="p">(</span><span class="s">"sock://localhost/8080"</span><span class="p">);</span>
    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_from_sock</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">,</span> <span class="mi">8080</span><span class="p">,</span> <span class="n">TB_SOCKET_TYPE_TCP</span><span class="p">,</span> <span class="n">tb_false</span><span class="p">);</span>

    <span class="c1">// 初始化udp流
</span>    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_from_url</span><span class="p">(</span><span class="s">"sock://localhost/8080?udp="</span><span class="p">);</span>
    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_from_sock</span><span class="p">(</span><span class="s">"localhost"</span><span class="p">,</span> <span class="mi">8080</span><span class="p">,</span> <span class="n">TB_SOCKET_TYPE_UDP</span><span class="p">,</span> <span class="n">tb_false</span><span class="p">);</span>

    <span class="c1">// 初始化数据流
</span>    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_from_url</span><span class="p">(</span><span class="s">"data://base64_data"</span><span class="p">);</span>
    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_from_data</span><span class="p">(</span><span class="n">data</span><span class="p">,</span> <span class="n">size</span><span class="p">);</span>

    <span class="c1">// 初始化字符集编码流
</span>    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_filter_from_charset</span><span class="p">(</span><span class="n">stream</span><span class="p">,</span> <span class="n">TB_CHARSET_TYPE_UTF8</span><span class="p">,</span> <span class="n">TB_CHARSET_TYPE_GBK</span><span class="p">);</span>

    <span class="c1">// 初始化gzip解压缩流
</span>    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_filter_from_zip</span><span class="p">(</span><span class="n">stream</span><span class="p">,</span> <span class="n">TB_ZIP_ALGO_GZIP</span><span class="p">,</span> <span class="n">TB_ZIP_ACTION_INFLATE</span><span class="p">);</span>
</code></pre>
</div>

<h4 id="非阻塞读取模式">非阻塞读取模式</h4>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 初始化http流
</span>    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_from_url</span><span class="p">(</span><span class="s">"http://www.baidu.com"</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">stream</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// 阻塞打开流，如果想在其他线程中断它，可以调用tb_stream_kill来实现
</span>        <span class="k">if</span> <span class="p">(</span><span class="n">tb_stream_open</span><span class="p">(</span><span class="n">stream</span><span class="p">))</span>
        <span class="p">{</span>
            <span class="cm">/* 判断流是否读取结束
             * 
             * 1. 如果这个流是能获取到文件大小的: 
             * tb_stream_size(stream) &gt;= 0 的情况下， 流读取偏移到流尾部，
             * beof 就直接返回tb_false 表示结束了。
             *
             * 2. 如果这个流是流式， 无法获取实际大小， 比如 http chunked、filter 流
             * tb_stream_size(stream) &lt; 0 的情况下
             * 这个时候 beof 永远是 tb_true， 流的结束 是通过 read 和 wait 来判断的。
             * 
             * 因此这种非阻塞读取模式是完全通用的，针对各种流模式。
             */</span>
            <span class="n">tb_byte_t</span> <span class="n">data</span><span class="p">[</span><span class="n">TB_STREAM_BLOCK_MAXN</span><span class="p">];</span>
            <span class="k">while</span> <span class="p">(</span><span class="n">tb_stream_beof</span><span class="p">(</span><span class="n">stream</span><span class="p">))</span>
            <span class="p">{</span>
                <span class="c1">// 非阻塞读取流数据， real 位实际读取到的大小，如果失败，则返回: -1
</span>                <span class="n">tb_long_t</span> <span class="n">real</span> <span class="o">=</span> <span class="n">tb_stream_read</span><span class="p">(</span><span class="n">stream</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">TB_STREAM_BLOCK_MAXN</span><span class="p">);</span>
                <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">real</span><span class="p">)</span>
                <span class="p">{</span>
                    <span class="c1">// 当前读取不到流数据，等待指定超时间隔的读取事件
</span>                    <span class="n">real</span> <span class="o">=</span> <span class="n">tb_stream_wait</span><span class="p">(</span><span class="n">stream</span><span class="p">,</span> <span class="n">TB_STREAM_WAIT_READ</span><span class="p">,</span> <span class="n">tb_stream_timeout</span><span class="p">(</span><span class="n">stream</span><span class="p">));</span>

                    <span class="c1">// 检测返回值，如果等待失败，返回：-1，或者等待超时，返回：0， 都对流进行结束读取处理
</span>                    <span class="n">tb_check_break</span><span class="p">(</span><span class="n">real</span> <span class="o">&gt;</span> <span class="mi">0</span><span class="p">);</span>
                <span class="p">}</span>
                <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="n">real</span> <span class="o">&lt;</span> <span class="mi">0</span><span class="p">)</span> <span class="k">break</span><span class="p">;</span>
            <span class="p">}</span>

            <span class="c1">// 关闭流，stream是可以支持重复打开关闭的
</span>            <span class="n">tb_stream_clos</span><span class="p">(</span><span class="n">stream</span><span class="p">);</span>
        <span class="p">}</span>

        <span class="c1">// 退出流，释放所有资源
</span>        <span class="n">tb_stream_exit</span><span class="p">(</span><span class="n">stream</span><span class="p">);</span>
    <span class="p">}</span>
</code></pre>
</div>

<h4 id="阻塞读取模式">阻塞读取模式</h4>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 初始化file流，支持windows、unix路径
</span>    <span class="n">tb_stream_ref_t</span> <span class="n">stream</span> <span class="o">=</span> <span class="n">tb_stream_init_from_url</span><span class="p">(</span><span class="s">"C://home/file"</span><span class="p">);</span>
    <span class="k">if</span> <span class="p">(</span><span class="n">stream</span><span class="p">)</span>
    <span class="p">{</span>
        <span class="c1">// 阻塞打开流，如果想在其他线程中断它，可以调用tb_stream_kill来实现
</span>        <span class="k">if</span> <span class="p">(</span><span class="n">tb_stream_open</span><span class="p">(</span><span class="n">stream</span><span class="p">))</span>
        <span class="p">{</span>
            <span class="c1">// 一次读取TB_STREAM_BLOCK_MAXN， 默认定义大小为：8192 
</span>            <span class="n">tb_byte_t</span> <span class="n">data</span><span class="p">[</span><span class="n">TB_STREAM_BLOCK_MAXN</span><span class="p">];</span>
            <span class="n">tb_hize_t</span> <span class="n">read</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span>

            <span class="cm">/* 获取流的大小
             * 注： tb_hong_t 是 tb_long_t 的升级版表示，也就是 tb_sint64_t
             * 注： tb_hize_t 是 tb_size_t 的升级版表示，也就是 tb_uint64_t
             *
             * 如果size &gt;= 0， 表示这个流是能够预先获取到大小的
             * 如果size &lt; 0， 表示这个流是完全流化，只能一直读直到读取中断，才能获取到实际大小
             *
             */</span>
            <span class="n">tb_hong_t</span> <span class="n">size</span> <span class="o">=</span> <span class="n">tb_stream_size</span><span class="p">(</span><span class="n">stream</span><span class="p">);</span>
            <span class="k">if</span> <span class="p">(</span><span class="n">size</span> <span class="o">&gt;=</span> <span class="mi">0</span><span class="p">)</span>
            <span class="p">{</span>
                <span class="k">while</span> <span class="p">(</span><span class="n">read</span> <span class="o">&gt;</span> <span class="n">size</span><span class="p">)</span>
                <span class="p">{</span>
                    <span class="c1">// 计算需要读取的大小
</span>                    <span class="n">tb_size_t</span> <span class="n">need</span> <span class="o">=</span> <span class="n">tb_min</span><span class="p">(</span><span class="n">TB_STREAM_BLOCK_MAXN</span><span class="p">,</span> <span class="n">size</span> <span class="o">-</span> <span class="n">read</span><span class="p">);</span>

                    <span class="c1">// 阻塞读取流数据， 如果失败，则返回: tb_false
</span>                    <span class="k">if</span> <span class="p">(</span><span class="o">!</span><span class="n">tb_stream_bread</span><span class="p">(</span><span class="n">stream</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">need</span><span class="p">))</span> <span class="k">break</span><span class="p">;</span>

                    <span class="c1">// 保存读取到的大小
</span>                    <span class="n">read</span> <span class="o">+=</span> <span class="n">need</span><span class="p">;</span>
                <span class="p">}</span>
            <span class="p">}</span>
            <span class="k">else</span>
            <span class="p">{</span>
                <span class="c1">// 需要非阻塞模式读取
</span>            <span class="p">}</span>

            <span class="c1">// 关闭流，stream是可以支持重复打开关闭的
</span>            <span class="n">tb_stream_clos</span><span class="p">(</span><span class="n">stream</span><span class="p">);</span>
        <span class="p">}</span>

        <span class="c1">// 退出流，释放所有资源
</span>        <span class="n">tb_stream_exit</span><span class="p">(</span><span class="n">stream</span><span class="p">);</span>
    <span class="p">}</span>
</code></pre>
</div>

<h4 id="阻塞读取一行数据">阻塞读取一行数据</h4>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 失败返回：-1， 成功返回实际读取到的行大小
</span>    <span class="n">tb_char_t</span> <span class="n">line</span><span class="p">[</span><span class="mi">8192</span><span class="p">]</span> <span class="o">=</span> <span class="p">{</span><span class="mi">0</span><span class="p">};</span>
    <span class="n">tb_long_t</span> <span class="n">real</span> <span class="o">=</span> <span class="n">tb_stream_bread_line</span><span class="p">(</span><span class="n">stream</span><span class="p">,</span> <span class="n">line</span><span class="p">,</span> <span class="mi">8192</span><span class="p">);</span>
</code></pre>
</div>

<h4 id="非阻塞模式写入">非阻塞模式写入</h4>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// real 位实际写入大小， 写入失败返回：-1
</span>    <span class="n">tb_long_t</span> <span class="n">real</span> <span class="o">=</span> <span class="n">tb_stream_writ</span><span class="p">(</span><span class="n">stream</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">size</span><span class="p">);</span>
</code></pre>
</div>

<h4 id="阻塞模式写入">阻塞模式写入</h4>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 写入失败返回 tb_false， 如果要中断写入，可以调用tb_stream_kill
</span>    <span class="n">tb_bool_t</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">tb_stream_bwrit</span><span class="p">(</span><span class="n">stream</span><span class="p">,</span> <span class="n">data</span><span class="p">,</span> <span class="n">size</span><span class="p">);</span>
</code></pre>
</div>

<h4 id="刷新同步数据到流">刷新同步数据到流</h4>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="cm">/* 同步结束数据到流，如果有尾部数据，则会写入， 一般在写完流结束是调用
     * 例如写gzip数据的尾部等等
     * 失败返回：tb_false
     */</span>
    <span class="n">tb_bool_t</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">tb_stream_sync</span><span class="p">(</span><span class="n">stream</span><span class="p">,</span> <span class="n">tb_true</span><span class="p">);</span>

    <span class="cm">/* 同步刷新数据到流，中间想强制刷新数据，则调用这个来写入 一般用于读入双通道的流
     * 例如写socket流包结束，想要等待接收读取时，强制刷新下写缓冲，开始进行读操作
     * 失败返回：tb_false
     */</span>
    <span class="n">tb_bool_t</span> <span class="n">ok</span> <span class="o">=</span> <span class="n">tb_stream_sync</span><span class="p">(</span><span class="n">stream</span><span class="p">,</span> <span class="n">tb_false</span><span class="p">);</span>
</code></pre>
</div>

<h4 id="阻塞写格式化字符数据到流">阻塞写格式化字符数据到流</h4>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 失败返回：-1， 成功返回实际写入的数据大小
</span>    <span class="n">tb_long_t</span> <span class="n">real</span> <span class="o">=</span> <span class="n">tb_stream_printf</span><span class="p">(</span><span class="s">"hello world: %s, %d</span><span class="se">\\</span><span class="s">n"</span><span class="p">,</span> <span class="s">"!"</span><span class="p">,</span> <span class="mi">12345678</span><span class="p">);</span>
</code></pre>
</div>

<h4 id="阻塞写入一行数据到流">阻塞写入一行数据到流</h4>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="c1">// 失败返回： -1,  成功返回实际写入大小
</span>    <span class="n">tb_long_t</span> <span class="n">real</span> <span class="o">=</span> <span class="n">tb_stream_bwrit_line</span><span class="p">(</span><span class="n">stream</span><span class="p">,</span> <span class="n">line</span><span class="p">,</span> <span class="n">size</span><span class="p">);</span>
</code></pre>
</div>

<h4 id="流的状态">流的状态</h4>

<p>如果遇到流读取失败，或者打开失败的情况，想要知道具体失败原因可以通过以下方式：</p>

<div class="language-c highlighter-rouge"><pre class="highlight"><code>    <span class="n">tb_size_t</span> <span class="n">state</span> <span class="o">=</span> <span class="n">tb_stream_state</span><span class="p">(</span><span class="n">stream</span><span class="p">);</span> 

    <span class="c1">// 将状态码转成字符串
</span>    <span class="n">tb_char_t</span> <span class="k">const</span><span class="o">*</span> <span class="n">state_cstr</span> <span class="o">=</span> <span class="n">tb_state_cstr</span><span class="p">(</span><span class="n">state</span><span class="p">);</span>
</code></pre>
</div>


        </article>
        <hr>

        <!-- baidu ads -->
        

        <!-- reward -->
        <div style="text-align: center;">
            <button id="rewardButton" disable="enable" onclick="var qr = document.getElementById('QR'); if (qr.style.display === 'none') {qr.style.display='block';} else {qr.style.display='none'}">
              <span>赏</span>
            </button>
            <div id="QR" style="display: none;">
                <div id="wechat" style="display: inline-block">
                  <img id="wechat_qr" src="/static/img/weixin.png" alt="WeChat Pay"/>
                  <p>微信打赏</p>
                </div>
                <div id="alipay" style="display: inline-block">
                  <img id="alipay_qr" src="/static/img/alipay.png" alt="Alipay"/>
                  <p>支付宝打赏</p>
                </div>
            </div>
        </div>

        
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
            
            
                
            
        
            
        
        

        
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
            
        
            
            
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
                
                    
                
                    
                
                    
                
            
            
        
        

        <div class="post-recent">
    <div class="pre">

        

        

        
        
        

        

        

        
        
        <p><strong>上一篇</strong> <a href="/cn/2016/08/24/forum-opened/">TBOOX开源社区论坛开通</a></p>
        
    </div>

    <div class="nex">

        

        

        
        
        <p><strong>下一篇</strong> <a href="/cn/2016/08/27/stream-architecture/">tbox stream流的架构介绍</a></p>
        
    </div>
</div>


        <h2 id="comments">评论</h2>
        






<div id="gitalk-container"></div>
<link rel="stylesheet" href="/css/gitalk.css">
<script src="/js/gitalk.min.js"></script>

<script>
const gitalk = new Gitalk({
  clientID: '73946dc1d9e2276ad0da',
  clientSecret: '12a3cb94361ba3ebc6ecb68cf80d592bfaa8106d',
  repo: 'tboox.github.io',
  owner: 'waruqi',
  admin: ['waruqi'],
  id: location.pathname,       
  language: 'zh-CN',
  distractionFreeMode: false  
})

gitalk.render('gitalk-container')
</script>





    </div>
    <button class="anchor"><i class="fa fa-anchor"></i></button>
    <div class="right">
        <div class="wrap">

            <!-- codefund ads -->
            

            <!-- Content -->
            <div class="side content">
                <div>
                    内容
                </div>
                <ul id="content-side" class="content-ul">
                    <li><a href="#english">English</a></li>
                    <li><a href="#similar_posts">相关文章</a></li>
                    <li><a href="#comments">评论</a></li>
                </ul>
            </div>


            <!-- baidu ads -->
            
            
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    链接
                </div>
                <ul class="content-ul">
                  <li><a href="http://github.com/waruqi/tbox">tbox</a></li>
                  <li><a href="http://www.xmake.io">xmake</a></li>
                  <li><a href="https://github.com/waruqi">github</a></li>
                </ul>
            </div> 

            <!-- xmake courses -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    xmake 入门课程
                </div>
                <a href="https://xmake.io/#/zh-cn/about/course" target="_blank">
                <img src="/static/img/xmake-course.png" alt="course" width="256" height="193">
                </a>
            </div>

            <!-- qqgroup -->
            <br>
            <div class="side">
                <div>
                    <i class="fa fa-external-link"></i>
                    技术交流群（QQ）
                </div>
                <img src="/static/img/qqgroup.png" alt="qqgroup" width="256" height="284">
            </div> 

            <!-- google ads -->
            

            <!-- baidu ads -->
            

        </div>
    </div>

    <!-- baidu ads -->
    
</div>
<script>
/**
 * target _blank
 */
(function() {
    var aTags = document.querySelectorAll('article a:not([id])')
    for (var i = 0; i < aTags.length; i++) {
        aTags[i].setAttribute('target', '_blank')
    }
}());
</script>
<script src="/js/pageContent.js " charset="utf-8"></script>



    <footer class="site-footer">
    <div class="wrapper">
        <p class="description">
             Copyright (c) 2016-2020 tboox.org 
        </p>
        <p class="contact">
            
            <a href="https://github.com/waruqi" title="GitHub"><i class="fa fa-github" aria-hidden="true"></i></a> 
             
            
            <a href="mailto:waruqi@gmail.com" title="email"><i class="fa fa-envelope-o" aria-hidden="true"></i></a> 
            
            
            <a href="https://twitter.com/waruqi" title="Twitter"><i class="fa fa-twitter" aria-hidden="true"></i></a> 
            
            <a href="/feed.xml" title="feed"><i class="fa fa-feed" aria-hidden="true"></i></a> 
        </p>
        <p class="power">
            <span>
                Site powered by <a href="https://jekyllrb.com/">Jekyll</a> & <a href="https://github.com/Gaohaoyang">HyG</a> & <a href="https://pages.github.com/">Github Pages</a>.
            </span>
        </p>
    </div>
</footer>

    <div class="back-to-top">
    <a href="#top" class="scroll">
        <i class="fa fa-arrow-up" aria-hidden="true"></i>
    </a>
</div>

    <script src=" /js/main.js " charset="utf-8"></script>
    <script src=" /js/scroll.min.js " charset="utf-8"></script>
  </body>

</html>
